package org.jeeasy.common.core.domain.model;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Getter;
import lombok.Setter;

/**
 * @author AlpsDDJ
 * @date 2021/8/9 16:31
 */
@Getter
@Setter
public class QueryPageModel implements QueryModel {

    private int size = 10;
    private int current = 1;
    private String sort;

    public boolean noSort() {
        return StrUtil.isBlank(sort) || StrUtil.EMPTY_JSON.equals(sort);
    }

    public void defaultSort(String field, String order) {
        if (noSort()) {
            this.sort = String.format("{\"%s\": \"%s\"}", field, order);
        }
    }

    public void defaultSort(String field) {
        defaultSort(field, QueryModel.ASC);
    }

    public <T> IPage<T> getPage() {
        return new Page<>(this.current, this.size);
    }

}
